﻿using System;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace n2n_client_runner
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool isRun;
            using (Mutex m = new Mutex(true, Application.ProductName, out isRun))
            {
                if (isRun)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());

                }
                else
                {
                    MessageBox.Show("程序已经运行");
                }
            }
        }
        
        public static void OpenTap()
        {
            Process myprocess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo("bin/tap-windows-9.21.2.exe");
            myprocess.StartInfo = startInfo;
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.Start();
            myprocess.WaitForExit();
            myprocess.Close();
        }

        public static string GetNatAdapterName()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            int index = 0;
            string name = "";
            foreach (NetworkInterface adapter in adapters)
            {
                //Console.WriteLine(adapter.Description);
                if (adapter.Description == "TAP-Windows Adapter V9")
                {
                    name = adapter.Name;
                }
                ++index;
            }
            //Console.WriteLine("Adapter Name:" + name);
            return name;
        }

        public static string GenerateMACAddress()
        {
            var sBuilder = new StringBuilder();
            var r = new Random();
            int number;
            byte b;
            for (int i = 0; i < 6; i++)
            {
                number = r.Next(0, 255);
                b = Convert.ToByte(number);
                if (i == 0)
                {
                    b = SetBit(b, 6);
                    b = UnsetBit(b, 7);
                }
                sBuilder.Append(number.ToString("X2"));
            }
            return sBuilder.ToString().ToLower().Insert(2, ":").Insert(5, ":").Insert(8, ":").Insert(11, ":").Insert(14, ":");
        }

        private static byte SetBit(byte b, int BitNumber)
        {
            if (BitNumber < 8 && BitNumber > -1)
            {
                return (byte)(b | (byte)(0x01 << BitNumber));
            }
            else
            {
                throw new InvalidOperationException(BitNumber.ToString());
            }
        }

        private static byte UnsetBit(byte b, int BitNumber)
        {
            if (BitNumber < 8 && BitNumber > -1)
            {
                return (byte)(b | (byte)(0x00 << BitNumber));
            }
            else
            {
                throw new InvalidOperationException(BitNumber.ToString());
            }
        }

    }
}
